package com.main.heath.servcie.impl;

import com.main.heath.mapper.PingdataExtendMapper;
import com.main.heath.po.PingData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by heathhose on 16-12-10.
 */
@Service
public class SavePingRCServiceImpl {

    public static boolean savePing(Connection con,String ping,Boolean reachable) throws UnknownHostException, SQLException {
        //ping数据举例：64 bytes from 123.207.88.18: icmp_seq=37 ttl=45 time=61.2 ms
        boolean flag=false;
        String str[] = ping.split(" ");
        PingData pingData = new PingData();
        pingData.setTranbytes(Integer.parseInt(str[0]));
        pingData.setStartip("");
        pingData.setToip(str[3].substring(0,str[3].length()-1));
        pingData.setTtl(Integer.parseInt(str[5].substring(4,str[5].length())));
        pingData.setPingtime(str[6].substring(5,str[6].length()));
        pingData.setReachable(reachable);

        if(savePingRC(con,pingData))
            flag = true;
        return flag;
    }



    public static  String getHostAddress() throws UnknownHostException {
        InetAddress ia = null;
        ia = ia.getLocalHost();

        return ia.getHostAddress();
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动类
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Detector", "root", "123");
            conn.setAutoCommit(false);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放数据库的资源
            return conn;
        }
    }

    public static boolean endSavePingRC(Connection conn){
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    public static boolean savePingRC(Connection con,PingData pingData) throws SQLException {
        Boolean flag = false;
        Statement st = con.createStatement();
        String sqlStr = null;
        sqlStr = "insert into pingData VALUES(" +
                "null"+","+
                "'"+pingData.getStartip() + "'," +
                "'"+pingData.getToip() + "'," +
                "now()" + "," +
                "'"+pingData.getPingtime() + "'," +
                pingData.getTtl() + "," +
                pingData.getTranbytes() + "," +
                "false ," +
                "false ," +
                pingData.getReachable() +");";//向数据库中插入数据
        flag = st.execute(sqlStr);
        con.commit();
        return flag;
    }

    public static void setPacketLoss(Connection con,String ip,String packetLoss){

        Statement st = null;
        try {
            st = con.createStatement();
            String sql = "insert into pingData(ToIP,pingStartTime,packetLoss) values('" +ip+"',now(),true);";
            int flag = st.executeUpdate(sql);
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    //保存无法到达的ping
    public static void saveOffPing(Connection con,String ip,Boolean reachable){
        Statement st = null;
        try {
            st = con.createStatement();
            String sql = "insert into pingData(ToIP,pingStartTime,reachable) values('" +ip+"',now(),true);";
            int flag = st.executeUpdate(sql);
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
